<template>
	<view class="certification">
		<view v-show="temp">
			<u--form labelPosition="left" :model="model1" :rules="rules" ref="uForm">
				<u-form-item label="头像" prop="avatarUrl" borderBottom ref="item1">
					<button open-type="chooseAvatar" @chooseavatar="chooseAvatar" class="btnAvatarUrl" plain="true">
						<u-avatar :src="model1.avatarUrl" size="50"></u-avatar>
					</button>
				</u-form-item>
				<u-form-item label="昵称" prop="nickName" borderBottom ref="item1">
					<u--input @blur="nickNameInput" type="nickname" v-model="model1.nickName" placeholder="请输入昵称"
						border="none"></u--input>
				</u-form-item>
				<u-form-item label="我的标识" prop="userCode" borderBottom ref="item1">
					<u--input disabled="true" type="userCode" v-model="model1.userCode" border="none"></u--input>
				</u-form-item>
				<u-form-item label="绑定课表" prop="bingCode" borderBottom ref="item1">
					<u--input type="bingCode" v-model="model1.bingCode" placeholder="(选填)输入你关注的用户标识"
						border="none"></u--input>
				</u-form-item>
				<u-form-item label="联系方式" prop="mobile" borderBottom ref="item1">
					<u--input v-model="model1.mobile" placeholder="(选填)请输入联系方式" border="none"></u--input>
				</u-form-item>
			</u--form>
			<u-button @click="submit" type="primary" text="保存"
				color="linear-gradient(to right, rgb(66, 83, 216), rgb(213, 51, 186))"></u-button>
			<navigator url="/page_my/pages/privacy" style="margin-top: 30rpx;">
				<view style="width: 100%;text-align: center;color: blue;text-decoration: underline;font-size: 28rpx;">
					《用户隐私协议》</view>
			</navigator>
			<privacy-popup ref="privacy" @confirm="confirm"></privacy-popup>
		</view>
		<view v-show="!temp">
			<u-empty text="同意返回即可" icon="https://cdn.uviewui.com/uview/empty/data.png">
			</u-empty>
		</view>
	</view>
</template>

<script>
	import {
		mapState,
		mapActions
	} from 'vuex'
	import {
		pathToBase64,
		base64ToPath
	} from 'image-tools'
	import {
		updateUserInfo,
		getUserInfo
	} from "@/plugins/api/userApi.js"
	import {
		privacyPopup
	} from "@/page_my/components/privacy-popup/privacy-popup.vue";
	export default {
		components: {
			privacyPopup
		},
		data() {
			return {
				// 用户隐私协议弹窗控制
				showPrivacy: getApp().globalData.showPrivacy,
				// 临时 -- 规避用户隐私
				temp: false,
				model1: {
					nickName: '',
					mobile: '',
					userCode: '',
					bingCode: '', // 绑定的个人标识
					avatarUrl: ''
				},
				rules: {
					'nickName': {
						type: 'string',
						required: true,
						message: '请填写昵称',
						trigger: ['blur', 'change']
					},
					"bingCode": {
						type: 'string',
						required: false
					},
					'mobile': [{
							type: 'string',
							required: false,
							message: '请输入手机号',
							trigger: ['blur', 'change'],
						},
						// {
						// 	// 自定义验证函数，见上说明
						// 	validator: (rule, value, callback) => {
						// 		// 上面有说，返回true表示校验通过，返回false表示不通过
						// 		// uni.$u.test.mobile()就是返回true或者false的
						// 		return uni.$u.test.mobile(value);
						// 	},
						// 	message: '手机号码不正确',
						// 	// 触发器可以同时用blur和change
						// 	trigger: ['blur', 'change'],
						// }
					]
				}
			}
		},
		onReady() {
			
		},
		onLoad(options) {
			this.getUserInfo()
		},
		mounted() {
			this.$nextTick(() => {
				console.log('DOM加载完毕')
				this.$refs.privacy.$refs.popup.open("centent")
				//onReady 为uni-app支持的生命周期之一
				this.$refs.uForm.setRules(this.rules)
			})
		},
		onShow() {
		},
		computed: {
			...mapState('user', ['openId'])
		},
		methods: {
			...mapActions('user', ['setUserInfoAction']),
			// 用户隐私确认
			confirm() {
				this.showPrivacy = false;
			},
			// 默认打开弹窗确认
			openPrivacy() {
				this.$refs.privacy.$refs.popup.open("centent")
			},
			nickNameInput(e) {
				this.model1.nickName = e
			},
			// 获得临时头像地址
			chooseAvatar(e) {
				pathToBase64(e.detail.avatarUrl).then(path => {
					this.model1.avatarUrl = path
				}).catch(err => {
					console.log(error)
				})
			},
			submit() {
				this.$refs.uForm.validate().then(res => {
					console.log(res)
					updateUserInfo({
						wxOpenId: this.openId,
						bingCode: this.model1.bingCode,
						userNick: this.model1.nickName,
						headUrl: this.model1.avatarUrl,
						userPhone: this.model1.mobile
					}).then(res => {
						getUserInfo({
							openId: this.openId
						}).then(ures => {
							this.setUserInfoAction(ures.data.data)
						})
						// 更新用户信息
						uni.$u.toast('信息更新成功')
					}).catch(err => {
						console.log(err)
						uni.$u.toast('信息更新异常，请更换小容量图片')
					})
				}).catch(errors => {
					uni.$u.toast('请检查输入是否有误')
				})
			},
			// 获取用户信息
			async getUserInfo() {
				getUserInfo({
					openId: this.openId
				}).then(async ures => {
					if (ures.data.status == 1) {
						// 用户不存在，需要注册
						uni.$u.toast('用户异常，请退出重试')
					} else {
						// 用户存在
						this.model1.nickName = ures.data.data.userNick
						this.model1.mobile = ures.data.data.userPhone
						this.model1.userCode = ures.data.data.userCode
						this.model1.bingCode = ures.data.data.bingCode
						this.model1.avatarUrl = ures.data.data.headUrl
						// --临时 规避用户隐私
						this.temp = ures.data.data.temp
						await this.setUserInfoAction(ures.data.data)
					}
				})
			}
		}
	}
</script>

<style scoped lang='scss'>
	page {
		background: #F8F8F8;
	}

	.certification {
		width: 690rpx;
		margin: 0 auto;

		.btnAvatarUrl {
			margin-right: 0;
			border: none;
		}
	}
</style>